package com.example.rocketmq.timewheel.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.rocketmq.timewheel.model.ScheduledTask;
import com.example.rocketmq.timewheel.service.TaskProducerService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 任务管理控制器
 */
@RestController
@RequestMapping("/api/task")
public class TaskController {
    
    @Resource
    private TaskProducerService taskProducerService;
    
    /**
     * 添加定时任务
     */
    @PostMapping("/add")
    public Map<String, Object> addScheduledTask(@RequestBody ScheduledTask task) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 添加任务到时间轮
            taskProducerService.addTaskToTimeWheel(task);
            
            result.put("success", true);
            result.put("message", "定时任务添加成功");
            result.put("taskId", task.getTaskId());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "定时任务添加失败: " + e.getMessage());
        }
        
        return result;
    }
}
